<?php

/**
 * appointment actions.
 *
 * @package    docplus.co.za
 * @subpackage appointment
 * @author     Fox Matuku
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class appointmentActions extends sfActions
{
    /**
     * Executes bookNowStepOne action
     *
     * @param sfRequest $request A request object
     */
    public function executeBookNowStepOne(sfWebRequest $request)
    {
        $this->doctorTimeSlotId = $request->getParameter('dts_id', null);
        $this->appointmentReasonId = $request->getParameter('appointment_reason_id', null);

        $this->forward404Unless($this->doctorTimeSlotId);
        $this->doctorTimeSlot = DoctorTimeSlotTable::getInstance()->findOneById($this->doctorTimeSlotId);

        $defaults = array();
        $defaults['time_slot_id'] = $this->doctorTimeSlotId;
        if($this->appointmentReasonId != 0){
            $defaults['appointment_reason_id'] = $this->appointmentReasonId;
        }
        
        $this->form = new BookNowForm($defaults);

        if ($request->isMethod('post'))
        {
            $this->form->bind($request->getParameter($this->form->getName()));
            if ($this->form->isValid())
            {
                try
                {
                    $this->getUser()->setBookNowDetails($this->form->getValues());

                    // decide what to do with the request
                    if ($this->getUser()->isAuthenticated())
                    {
                        $this->redirect("/appointment-book-now-step-three");
                    }
                    else
                    {
                        $this->redirect("/appointment-book-now-step-two/none");
                    }
                }
                catch (Exception $e)
                {
                    
                }
            }
        }
    }

    /**
     * Executes bookNowStepTwo action
     *
     * @param sfRequest $request A request object
     */
    public function executeBookNowStepTwo(sfWebRequest $request)
    {
        $this->myEmailForm = new MyEmailForm();
        $this->signInForm = new SignInForm();
        $this->resetPassword = new ResetPasswordForm();

        if ($request->isMethod('post'))
        {
            // check to see if the user is trying to sign up
            if ($request->hasParameter("option") && $request->getParameter("option") == "signup")
            {
                $this->myEmailForm->bind($request->getParameter($this->myEmailForm->getName()));
                if ($this->myEmailForm->isValid())
                {
                    try
                    {
                        // grab the book now details from the user session
                        $bookNowDetails = $this->getUser()->getBookNowDetails(array());
                        $bookNowDetails['email'] = $this->myEmailForm->getValue('email');

                        // push the modified booking details to the user session back
                        $this->getUser()->setBookNowDetails($bookNowDetails);

                        //proceed to the Member register page
                        $this->redirect("/member-register-step-one");
                    }
                    catch (Exception $e)
                    {
                        
                    }
                }
            }
            else if ($request->hasParameter("option") && $request->getParameter("option") == "signin")
            {
                $this->signInForm->bind($request->getParameter($this->signInForm->getName()));
                if ($this->signInForm->isValid())
                {
                    try
                    {
                        // seems everything has gone smoothly...so we can as well log them in :)
                        $this->getUser()->signin($this->signInForm->getValue('user'));

                        //proceed to the step three book now page
                        $this->redirect("/appointment-book-now-step-three");
                    }
                    catch (Exception $e)
                    {
                        
                    }
                }
            }
            else if ($request->hasParameter("option") && $request->getParameter("option") == "resetpassword")
            {
                $this->resetPassword->bind($request->getParameter($this->resetPassword->getName()));
                if ($this->resetPassword->isValid())
                {
                    try
                    {
                        //users should be able to receive an email with their new loging details
                    }
                    catch (Exception $e)
                    {
                        
                    }
                }
            }
        }
    }

    /**
     * Executes bookNowStepThree action
     *
     * @param sfRequest $request A request object
     */
    public function executeBookNowStepThree(sfWebRequest $request)
    {
        // grab the book now details from the user session
        $this->bookNowDetails = $this->getUser()->getBookNowDetails(null);
        $this->redirectUnless($this->bookNowDetails, "@home");

        if (isset($this->bookNowDetails['time_slot_id']))
        {
            $this->doctorTimeSlot = DoctorTimeSlotTable::getInstance()->findOneById($this->bookNowDetails['time_slot_id']);
            $doctorId = $this->doctorTimeSlot->getDoctorId();
        }

        if (isset($this->bookNowDetails['appointment_reason_id']))
        {
            $appointmentReason = AppointmentReasonTable::getInstance()->findOneById($this->bookNowDetails['appointment_reason_id']);
            $this->bookNowDetails['appointment_reason'] = $appointmentReason->getName();
        }

        $this->bookNowDetails['insurance'] = (isset($this->bookNowDetails['is_using_insurance']) && $this->bookNowDetails['is_using_insurance'] == 1) ? "I will use a medical aid" : "I'm paying by myself";

        // push back all the book now details to the user session
        $this->getUser()->setBookNowDetails($this->bookNowDetails);
        $appointment = new Appointment();
        $appointment->setTimeSlotId($this->bookNowDetails['time_slot_id']);
        $appointment->setDoctorId($doctorId);
        $appointment->setMemberId($this->getUser()->getMember()->getId());
        $appointment->setAppointmentReasonId($this->bookNowDetails['appointment_reason_id']);

        $this->form = new myMemberAppointmentForm($appointment);

        if ($request->isMethod('post'))
        {
            $this->form->bind($request->getParameter($this->form->getName()));
            if ($this->form->isValid())
            {
                try
                {
                    // save the appointment
                    $appointment = $this->form->save();

                    // send the appointment notication to the doctor
                    // sent an appointment placement confirmation to the Member
                    //proceed to step two
                    $this->redirect("/appointment-book-now-step-four");
                }
                catch (Exception $e)
                {                    
                }
            }
        }
    }

    /**
     * Executes bookNowStepFour action
     *
     * @param sfRequest $request A request object
     */
    public function executeBookNowStepFour(sfWebRequest $request)
    {
        // grab the book now details from the user session
        $this->bookNowDetails = $this->getUser()->getBookNowDetails(null);
        $this->redirectUnless($this->bookNowDetails, "@home");

        if (isset($this->bookNowDetails['time_slot_id']))
        {
            $this->doctorTimeSlot = DoctorTimeSlotTable::getInstance()->findOneById($this->bookNowDetails['time_slot_id']);
            $doctorId = $this->doctorTimeSlot->getDoctorId();
        }

        // clear the book now details off the user session
        $this->getUser()->setBookNowDetails(null);
    }

    /**
     * Executes bookNow action
     *
     * @param sfRequest $request A request object
     */
    public function executeBookNow(sfWebRequest $request)
    {
        $doctorId = $request->getParameter('doctor_id', null);
        $this->forward404Unless($doctorId);

        $this->doctor = DoctorTable::getInstance()->findOneById($doctorId);
        $this->form = new myAppointmentReasonInlineForm();
    }

}
